package test;

import jtarget.data.model.ShootingLane;
import jtarget.server.ServiceData;
import jtarget.server.view.ViewActions;
import jtarget.view.board.ScreenBoard;
import jtarget.view.board.SmallTargetBoardView;
import org.apache.log4j.Logger;

import javax.swing.*;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.List;

public class GroupListModel implements ListModel, ListSelectionListener, MouseListener {
    static Logger logger = Logger.getLogger(GroupListModel.class.getName());
    public int groupNum;
    public int targetNum;
    List<SmallTargetBoardView> laneList = new LinkedList<SmallTargetBoardView>();
    int selctedBoardIndex = 0;

    public GroupListModel(int row, int col) {
        this.groupNum = row;
        this.targetNum = col;
        SmallTargetBoardView view = new SmallTargetBoardView();
        view.screenSize = ScreenBoard.SIZE_Preview;
        ShootingLane session = ServiceData.getInstance().getShootingLane(1);
        view.setScreenBoardModel(session);
        view.initialize();
        for (int i = 0; i < row * col; i++) {
            laneList.add(view);
        }
    }

    public Object getElementAt(int index) {
        return laneList.get(index);
    }

    public int getSize() {
        return laneList.size();
    }

    public void addListDataListener(ListDataListener l) {
    }

    public void removeListDataListener(ListDataListener l) {
    }

    public void valueChanged(ListSelectionEvent e) {
        logger.debug("valueChanged, e=" + e.toString());

        if (e.getValueIsAdjusting()) {
            logger.debug("valueChanged, getValueIsAdjusting()=true, return");
            return;
        }

        UpdateView((JList) e.getSource());
    }

    private void UpdateView(JList theList) {
        if (theList.isSelectionEmpty()) {
            logger.debug("UpdateView, isSelectionEmpty()=true, return");
        } else {
            selctedBoardIndex = theList.getSelectedIndex();
            logger.debug("UpdateView, selctedBoardIndex=" + selctedBoardIndex);
            ViewActions.selectShootingLane(selctedBoardIndex);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            UpdateView((JList) e.getSource());
        }
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
}
